ndla-util
Collection of util functions used by NDLA
Installation
yarn add @ndla/util
npm install @ndla/util
Usage
import uuid from '@ndla/util/uuid';
const unique = uuid();
console.log(unique);
or
import { uuid } from '@ndla/util';
const unique = uuid();
console.log(unique);
API(functions)
uuid()
Get an unique identifier.
const unique = util.uuid();
console.log(unique);
getComponentName()
Get component name of a React component. Useful in HOCs.
import { getComponentName } from '@ndla/util';
const withHOC = (WrappedComponent) => {
class NameOfHoc extends Component {
....
render() {
return createElement(WrappedComponent, nextProps);
}
}
NameOfHoc.displayName = `NameOfHoc(${getComponentName(WrappedComponent)})`;
return hoistNonReactStatics(NameOfHoc, WrappedComponent);
};
export default withStateHandler;
copyTextToClipboard(text)
Copies text to clipboard by (1) creating a hidden textarea with the provided text, (2) selects the text, (3) runs document.execCommand('copy')
.
import { copyTextToClipboard } from '@ndla/util';
copyTextToClipboard('Hello world!');
tagsI18N(object, locale, withFallback, preferdLocales)
Finds translation for a specific language in an array named tags from an object. Prefered locales is a fallback option if the given locale translation does not exist.
If no preferdLocales is defined, it is default sat to ['nb', 'nn', 'en']
.
import { tagsI18N } from '@ndla/util';
tagsI18N(object, 'nb', false, ['nb', 'nn', 'en']);